package cc.wsyw126.java.lintCode;

import java.util.ArrayList;
import java.util.Stack;

/**
 * write by yangDP
 * 2015.10.3
 */

public class convertToPN {

    public static void main(String[] args) {
        String[] expression = {"2", "*", "6", "-", "(", "23", "+", "7", ")", "/", "(", "1", "+", "2", ")"};
        int length = expression.length;
        ArrayList<String> list = new ArrayList<String>();
        Stack<String> S = new Stack<String>();
        for (int i = length - 1; i >= 0; i--) {
            if (Character.isDigit(expression[i].charAt(0))) {
                list.add(expression[i]);
            } else {
                switch (expression[i].charAt(0)) {
                    case ')':
                        S.push(expression[i]);
                        break;
                    case '+':
                    case '-':
                        while (!S.isEmpty()) {
                            if (S.peek().charAt(0) == '*' || S.peek().charAt(0) == '/') list.add(S.pop());
                            else break;
                        }
                        S.push(expression[i]);
                        break;
                    case '*':
                    case '/':
                        S.push(expression[i]);
                        break;
                    case '(':
                        while (!S.isEmpty()) {
                            if (S.peek().charAt(0) != ')') list.add(S.pop());
                            else {
                                S.pop();
                                break;
                            }
                        }
                        break;
                }
            }
        }
        while (!S.isEmpty())
            list.add(S.pop());
        ArrayList<String> L = new ArrayList<String>();
        for (int i = list.size() - 1; i >= 0; i--)
            L.add(list.get(i));
        System.out.print(L);
    }

}
